/*
 *  Copyright 2010 UPF Telecos 4th group G.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package edu.upf.listeners;

import edu.upf.content.DescriptionContent;
import edu.upf.core.ClientLogic;
import edu.upf.network.impl.IncomingContentTransfer;
import edu.upf.utils.Definitions;
import edu.upf.utils.LogSystem;
import java.io.File;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.filetransfer.FileTransferListener;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;

/**
 * Implements the listener logic when a media transfer petition message
 * is received
 * @author netjois
 */
public class MediaTransferListener implements FileTransferListener, Definitions {

    private String transferID;
    private ClientLogic clientLogic;
    private boolean notifyMode;
    private LogSystem log;

    /**
     * Constructor of the listener, received a client logic object for
     * callbacks and the transferID to accept the incoming transfer
     * @param clientLogic
     * @param transferID
     * @param notifyMode 
     */
    public MediaTransferListener(ClientLogic clientLogic, String transferID,
            boolean notifyMode) {
        this.transferID = transferID;
        this.clientLogic = clientLogic;
        this.notifyMode = notifyMode;
        log = new LogSystem(this.getClass());
    }

    /**
     * Method called when the request is received, decides if is has to be
     * accepted, stores the data and notifies the client logic
     * @param request
     */
    @Override
    public void fileTransferRequest(FileTransferRequest request) {
        // Check to see if the request should be accepted
        DescriptionContent content = clientLogic.createDescriptionContent(transferID);
        String fileID=content.getName()+"."+content.getFormat();
        if (fileID.equals(request.getDescription())) {
            try {
                IncomingFileTransfer transfer = request.accept();
                String path;
                if(notifyMode){
                    path=TEMP_FOLDER;
                    (new File(path)).mkdir();
                }else{
                    path=DOWNLOADS_FOLDER;
                }
                String downloadedFilePath=path+request.getDescription();
                File file = new File(path+request.getDescription());
                transfer.recieveFile(file);
                IncomingContentTransfer incomingTransfer = new IncomingContentTransfer(transfer);
                clientLogic.addContentTransfer(incomingTransfer);
                while (transfer.getProgress()<0.9999) {
                    try {
                        Thread.sleep(DOWNLOADS_REFRESH_INTERVAL);
                    } catch (InterruptedException ex) {
                        clientLogic.showExceptionMessage(ex, ERROR);
                    }
                }
                clientLogic.processFinishedDownload(notifyMode, downloadedFilePath);
            } catch (XMPPException ex) {
                clientLogic.showExceptionMessage(ex, ERROR);
            }
        } else {
            request.reject();
            log.addInfoLog("Transfer rejected");
        }
    }
}
